package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.ScenicSpot;
import cn.wolfcode.luowowo.article.query.SpotQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicSpotService;
import cn.wolfcode.luowowo.cache.service.IScenicSpotVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.comment.domain.SpotComment;
import cn.wolfcode.luowowo.comment.query.SpotCommentQuery;
import cn.wolfcode.luowowo.comment.service.ISpotCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annoation.LoginCheck;
import cn.wolfcode.luowowo.website.annoation.UserParam;
import cn.wolfcoe.luowowo.common.util.baiducheck.SensitiveWordsUtil;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@RequestMapping("/scenicspot")
@Controller
public class ScenicSpotController {
    @Reference
    private IScenicSpotService scenicSpotService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IScenicSpotVOService scenicSpotVOService;

    @Reference
    private ISpotCommentService spotCommentService;

    @RequestMapping("")
    public String index(Model model,Long destId) {
        List<Destination> toasts = destinationService.getToasts(destId);
        Destination destination = toasts.remove( toasts.size() - 1 );
        model.addAttribute( "toasts" ,toasts);
        model.addAttribute( "dest" ,destination);
        List<ScenicSpot> topfive = scenicSpotService.queryTop5(destId);
        model.addAttribute( "topfive", topfive);
        model.addAttribute("hotspot",scenicSpotService.hotspot(destId));
        model.addAttribute("tags",scenicSpotService.tagsAll());
        return "scenicspot/list";
    }

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")SpotQuery qo){
        PageInfo pageInfo = scenicSpotService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "scenicspot/spotpage";
    }

    @RequestMapping("/detail")
    public String detail(Model model,Long id){
        ScenicSpot spotdetail = scenicSpotService.get(id);
        List<Destination> toasts = destinationService.getToasts(spotdetail.getDest().getId());
        Destination destination = toasts.remove( toasts.size() - 1 );
        model.addAttribute( "toasts" ,toasts);
        model.addAttribute( "dest" ,destination);
        model.addAttribute("spotdetail",spotdetail);
        model.addAttribute("children",scenicSpotService.getChild(id));

        return "/scenicspot/detail";
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Model model, Long spotid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult( false, "请先登录" );
            result.setCode( 102 );
            return result;
        }
        Boolean flag = scenicSpotVOService.favor( spotid, userInfo );
        String zetKey = RedisKeys.SCENIC_SPOT_ZSET_COMMEND.getPrifx();

        return new AjaxResult( flag ).addData( scenicSpotVOService.getScenicSpotVO( spotid ) );
    }

    @RequestMapping("/been")
    @ResponseBody
    public Object been(Model model, Long spotid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult( false, "请先登录" );
            result.setCode( 102 );
            return result;
        }
        Boolean flag = scenicSpotVOService.been( spotid, userInfo.getId() );


        return new AjaxResult( flag ).addData( scenicSpotVOService.getScenicSpotVO( spotid ) );
    }

    @RequestMapping("/commentAdd")
    @LoginCheck
    public String saveOrUpdate(Model model, SpotComment comment, @UserParam UserInfo userInfo) {



        comment.setUserId( userInfo.getId() );
        comment.setUsername( userInfo.getNickname() );
        comment.setUserheadUrl( userInfo.getHeadImgUrl() );
        comment.setUserlevel( userInfo.getLevel() );
        comment.setContent( SensitiveWordsUtil.SensitiveWordCheck( comment.getContent() ).getContent() );
        comment = spotCommentService.saveOrUpdate( comment );
        model.addAttribute( "c", comment );
        //返回当前foolr


        long floor = spotCommentService.getCount( comment.getSpotId() );
        model.addAttribute( "floor", floor );
        return "scenicspot/commentTpl";
    }

    /**
     * 景点去全部论数据
     * @param qo
     * @return
     */
    @RequestMapping("/comment")
    @ResponseBody
    public Object comment( @ModelAttribute("qo") SpotCommentQuery qo) {

        qo.setSpotId(1L);
        Page<SpotComment> page = spotCommentService.query( qo );
        for (SpotComment spotComment : page.getContent()) {
            System.err.println(spotComment.getUsername()+":"+spotComment.getContent());
        }
        //model.addAttribute( "sclist", page.getContent() );
        return page.getContent();
    }
    @RequestMapping("zoajia")
    @ResponseBody
    public Object init(@UserParam UserInfo userInfo){
        SpotComment comment = new SpotComment();
        comment.setSpotId(5L);
        comment.setUserlevel(1);
        comment.setUserheadUrl(userInfo.getHeadImgUrl());
        comment.setUsername(userInfo.getNickname());
        comment.setContent("小惠最漂亮");
        comment.setTime(new Date());
        comment.setUserId(userInfo.getId());
        comment.setFengguang(4);
        comment.setFuwu(4);
        comment.setPingjia(4);
        comment.setTese(4);
        comment.setThumpUpId(userInfo.getId());
        comment.setThumpUpNum(50);
        comment.setType(SpotComment.SPOT_COMMENT_TYPE_COMMENT);
        spotCommentService.saveOrUpdate(comment);
        return "ok";
    }

}
